#include "../includes.h"


COMMAND_	COMMAND;


void Init_COMMAND(void);
void ExecuteCommand(char);
void ReplyCommand(char);
void SetCommand(char);

void new_COMMAND()
{
	//New_Command()
}
void new_COMMAND_2()
{
	//New_Command()
}

void Init_COMMAND()
{
	Init_OdeticCommand();
	
	COMMAND.CTRL_PORT.SERVICE 		= CMD_PRE_INPUT;
	COMMAND.CTRL_PORT.COMMAND_SET 	= NAVICOM_COMMAND;
	COMMAND.CTRL_PORT.Input[0] 		= 0;
	COMMAND.CTRL_PORT.Output[0] 	= 0;
	COMMAND.CTRL_PORT.Count 		= 0;
	COMMAND.CTRL_PORT.Current 		= 0;

	COMMAND.COMMON_PORT = COMMAND.CTRL_PORT;

	//DisableControlPortTx();
}



void ExecuteCommand(char port)
{
	char result;


    Command_	*Command;
	PORT_		*PORT;

	switch(port){
		case PORT_CONTROL:
					Command = &COMMAND.CTRL_PORT;
					PORT 	= &SYSTEM.IO.CTRL_PORT;
					break;
		case PORT_COMMON:
		default:
					Command = &COMMAND.COMMON_PORT;
					PORT 	= &SYSTEM.IO.COMMON_PORT;
					break;
	}



	if(Command->Current < Command->Count ){

		switch((int)Command->Queue[Command->Current].CommandSet){
			case NAVICOM_COMMAND:

					//if(port == PORT_COMMON){TransmitMessage("!",PORT_CONTROL);}

				result = DoNAVICOMCommand( (int)Command->Queue[Command->Current].ID ,Command->Queue[Command->Current].Parameter ,Command->Queue[Command->Current].CallCount ,port);
				break;
			case Odetic_COMMAND:
				//kang if(tod_tx_ready) return;
					//if(port == PORT_COMMON){TransmitMessage("?",PORT_CONTROL);}

				result = DoOdeticCommand(Command->Queue[Command->Current].ID,Command->Queue[Command->Current].Parameter,Command->Queue[Command->Current].CallCount,port);
				break;
		}

		if(result == EXE_ING ){

			Command->Queue[Command->Current].CallCount++;
		} else {

			Command->Current++;
		}
	}


	if( Command->Current == Command->Count){

		if( result == EXE_ERROR ){
			strcpy(Command->Output,"Semantic Error\r\n");
		}


		if( strlen(Command->Output) > 0 ) {
			Command->SERVICE = CMD_REPLY;
		}

		else {
	    	Command->SERVICE = CMD_PRE_INPUT;
			Command->Input[0] = 0;
			//Command->Output[0] = NULL;
		}
	}
}

int iReplyCommandCnt=0;
void ReplyCommand(char port)
{

    Command_	*Command;
	PORT_		*PORT;
	int length;

	switch(port){
		case PORT_CONTROL:
					Command = &COMMAND.CTRL_PORT;
					PORT 	= &SYSTEM.IO.CTRL_PORT;
					break;
		case PORT_COMMON:
		default:
					Command = &COMMAND.COMMON_PORT;
					PORT 	= &SYSTEM.IO.COMMON_PORT;
					break;
	}



//	sprintf((char*)MESG,Command->Output);
	
////	if( port == PORT_CONTROL){
//	    if(PORT->MODE == UCOMMAND && PORT->PROMPT == ON){
//    	    strcat((char*)MESG,PROMPT);
//   	 	}
	length = strlen(Command->Output);
   	 
	if( length >=0 && (length + (PROMPT_LENGTH+1) < 2048) ){
   		sprintf((char*)MESG,Command->Output);	
	}
	
	length = strlen(MESG);
	
   	if(PORT->MODE == UCOMMAND && PORT->PROMPT == ON && 
   		(length + (PROMPT_LENGTH+1) < 2048)){
		//strcat((char*)MESG,PROMPT);
	}
//   	}

 	
   	if(strlen((char*)MESG)>0){
   		TransmitMessage((BYTE*)MESG,port);
   	}

    Command->SERVICE = CMD_PRE_INPUT;
	Command->Input[0] = 0;
	Command->Output[0] = 0;

    switch(port){
    	case	PORT_CONTROL:{	
    				scc3_rx_cnt = 0;	
    				//printf("\nGRCA-%c>",(char)('A'+SYSTEM.ID));  //,iReplyCommandCnt++ 
					//fflush (stdout);
					break; 
				}
    	case	PORT_COMMON:	scc1_rx_cnt = 0;	break;
    }
}




// Command SET
// NAVICOM_Command
void SetCommand(char port)
{
    register int i;
    char CommandChar;

    int 			*RxCount;
    unsigned char 	*CommandBuffer;
    Command_		*Command;


	switch(port){

		case PORT_CONTROL:
				RxCount = &scc3_rx_cnt;
				CommandBuffer = CommandBuffer3;
				Command = &COMMAND.CTRL_PORT;
				break;
		case PORT_COMMON:
				RxCount = &scc1_rx_cnt;
				CommandBuffer = CommandBuffer1;
				Command = &COMMAND.COMMON_PORT;
				break;
	}


    CommandChar = ' ';	//NON-command

	for( i = 0; i < *RxCount; i++){
        if(CommandBuffer[i] != ' '){
        	CommandChar = CommandBuffer[i];
        	//U0putc(CommandChar);
        	break;
        }
    }

	//Command->COMMAND_SET = NAVICOM_COMMAND;

	//===============================================================
	//	UCT Command
	//===============================================================
    switch(CommandChar){
       	case '$':	Command->COMMAND_SET = Odetic_COMMAND;
     				break;
		default :
					Command->COMMAND_SET = NAVICOM_COMMAND;
	    			break;
    }
    
//    if(port == PORT_COMMON){
//		Command->COMMAND_SET = Odetic_COMMAND;
//	}
//	else{
//		Command->COMMAND_SET = NAVICOM_COMMAND;
//	}
}

